package com.waimai.mobile.ui.activity;

import android.view.View;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.Toast;

import com.waimai.mobile.R;
import com.waimai.mobile.api.WaiMaiServer;
import com.waimai.mobile.api.entity.Comment;
import com.waimai.mobile.api.entity.Order;
import com.waimai.mobile.api.model.ResponseModel;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.rest.RestService;

/**
 * Created by lin on 2015/6/22.
 */
@EActivity(R.layout.activity_comment)
public class CommentActivity extends BaseActivity02{

    @Extra("order")
    public Order order;

    @RestService
    public WaiMaiServer server;

    @ViewById
    public RatingBar starLevelRb;

    @ViewById
    public EditText commentContentEdt;

    @AfterViews
    public void afterViews(){
        setActionBarTitle("评价订单");
    }

    @Click(R.id.submitCommentBtn)
    public void submitCommentBtnClick(View v){
        Comment comment = new Comment();

        String content = commentContentEdt.getText().toString().trim();
        if(content != null && !"".equals(content)){
            comment.setContent(content);
        }
        comment.setStarLevel(starLevelRb.getRating());

        submitComment(order, comment);
    }

    @Background
    public void submitComment(Order order, Comment comment){
        try{
            ResponseModel<Comment> model = server.submitComment(order.getUser().getUserId(), order.getOrderId(), comment);
            if(model != null){
                switch (model.getStatus().getStatusCode()){
                    case ResponseModel.STATUS_REQUEST_SUCCESS:
                        requestSuccess();
                        break;
                    case ResponseModel.STATUS_REQUEST_FAILED:
                        requestFailed();
                        break;
                    case ResponseModel.STATUS_SERVER_ERROR:
                        serverError();
                        break;
                }
            }else{
                serverError();
            }
        }catch (Exception e){
            e.printStackTrace();
            netError();
        }
    }

    @UiThread
    public void requestSuccess(){
        Toast.makeText(this, "操作成功", Toast.LENGTH_SHORT).show();
        onBackPressed();
    }

    @UiThread
    public void requestFailed(){
        Toast.makeText(this, "操作失败，请重试", Toast.LENGTH_SHORT).show();
    }


    @UiThread
    public void serverError(){
        Toast.makeText(this, "服务器错误，请重试", Toast.LENGTH_SHORT).show();
    }

    @UiThread
    public void netError(){
        Toast.makeText(getApplicationContext(), "网络错误，请重试", Toast.LENGTH_SHORT).show();
    }

}
